package cn.tedu.study.usermodel.controller;

import cn.tedu.study.response.ResultVO;
import cn.tedu.study.response.StatusCode;
import cn.tedu.study.security.CustomUserDetail;
import cn.tedu.study.usermodel.entity.vo.FriendsListVo;
import cn.tedu.study.usermodel.mapper.FriendsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author:yuziji
 * @create: 2023-06-10 12:37
 * @description:
 */
@RestController
@RequestMapping("/v1/friends")
public class FriendsController {
    @Autowired
    private FriendsMapper friendsMapper;

    @RequestMapping("/selectAllFriends")
    public ResultVO selectAllFriends(@AuthenticationPrincipal CustomUserDetail userDetail) {
        if (userDetail == null) {
            return ResultVO.ok(StatusCode.NOT_LOGIN);
        }
        System.out.println(userDetail);
        List<FriendsListVo> friendsListVoList = friendsMapper.selectAllFriends(userDetail.getId());
        System.out.println(friendsListVoList);
        return ResultVO.ok(friendsListVoList);
    }

    @RequestMapping("/deleteByUserAndFriendId/{friendId}")
    public ResultVO deleteByUserAndFriendId(@PathVariable("friendId") Integer friendId, @AuthenticationPrincipal CustomUserDetail userDetail) {
        System.out.println(friendId + "=====" + userDetail.getId());
        friendsMapper.updateByUserAndFriendId(friendId, userDetail.getId());
        return ResultVO.ok();
    }
}
